{% extends "../../main.html" %}

{% block title %}{{ _("Statistics") }}{% end %}

{% block header %}
<script src="/static/js/pages/admin/view/statistics.js"></script>
<script src="/static/js/pages/anchor.js"></script>
{% end %}

{% block modals %}
{% from tornado.options import options %}
{% from models.Flag import Flag %}
    <!-- View Details -->
    <div id="details-flag-modal" class="modal hide fade" style="display: none; width:760px; margin-left:-380px;">
        <div class="modal-header" style="padding-bottom: 0; margin-bottom: 0;">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3><i class="fa fa-fw fa-flag"></i> {{ _("Flag Details") }}<span id="flag_value" style="float: right; font-size: 80%; padding-right: 50px; margin-top: 6px;"></span></h3>
            <h5>
                <table class="table">
                    <tbody id="details-flag">
                        <tr>
                            <td class="shortcolumn" style="padding-left: 30px;">
                                <br><strong>Name:&nbsp;&nbsp;</strong><span id="details_flag_name"></span>
                            </td>
                            <td class="descriptioncol" style="float: right; padding-right: 30px;">
                                <strong>Description:&nbsp;&nbsp;</strong><span id="details_flag_description"></span>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" class="descriptioncol" style="padding-top: 0 !important; padding-left: 30px;">
                                <strong>Token:&nbsp;&nbsp;</strong><span id="details_flag_token"></span></span>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table class="table" style="margin-left: -25px;">
                    <tr>
                        <td class='shortcolumn' style='width 50px; text-align: center;'>
                            <i class='fa fa-fw fa-times' style='color: rgb(200, 0, 0);'></i>
                            <strong>Attempts:&nbsp;&nbsp;</strong><span id="count_attempts"></span>
                        </td>
                        <td class='shortcolumn' style='width 50px; text-align: center;'>
                            <i class="fa fa-fw fa-check" style="color: green;"></i>
                            <strong>Captures:&nbsp;&nbsp;</strong><span id="count_captures"></span>
                        </td>
                        <td class='shortcolumn' style='width 50px; text-align: center;'>
                            <i class="fa fa-fw fa-medkit" style="color: rgb(200, 200, 0);"></i>
                            <strong>Hintes:&nbsp;&nbsp;</strong><span id="count_hints"></span>
                        </td>
                    </tr>
                </table>
            </h5>
        </div>
        <hr  style="border-top: 1px solid #333; margin-top: -5px; margin-bottom: 21px; margin-left: 15px; margin-right: 30px;"/>
        <div class="modal-body" style="padding-top: 0; margin-top: -20px;">
            <h4><i class="fa fa-fw fa-times" style="color: rgb(200, 0, 0);"></i> {{ _("Attempts") }}</h4>
            <div class="well" style="padding-bottom: 0px;">
                <table class="table table-striped">
                    <tbody id="details-attempts" style="width: 700px;"></tbody>
                </table>
            </div>
            <h4><i class="fa fa-fw fa-check" style="color: green;"></i> {{ _("Captures") }}</h4>
            <div class="well" style="padding-bottom: 0px;">
                <table class="table table-striped">
                    <tbody id="details-captures"></tbody>
                </table>
            </div>
            <h4>&nbsp;<i class="fa fa-fw fa-medkit" style="color: rgb(200, 200, 0);"></i> {{ _("Hints") }}</h4>
            <div class="well" style="padding-bottom: 0px;">
                <table class="table table-striped">
                    <tbody id="details-hints"></tbody>
                </table>
            </div>
        </div>
    </div>
    <!-- Accept Answer -->
    <div id="details-answer-modal" class="modal hide fade" style="display: none; width:560px; margin-left:-280px;">
        <div class="modal-header" style="padding-bottom: 0; margin-bottom: 0;">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3><i class="fa fa-fw fa-flag"></i> {{ _("Accept Answer") }}</h3>
            <h5>
                <table class="table">
                    <tbody id="details-flag"></tbody>
                </table>
            </h5>
        </div>
        <div class="modal-body">
            <div style="margin: 10px 0 30px 25px; vertical-align: middle;">
                <h4>
                    <div id="answer-team" style="margin-left: 25px;"></div>
                </h4>
                <div class="descriptioncol" id="answer-flag" style="margin-left: 25px;text-align: center;"></div>
            </div>
            <div id="flagtypenote" style="display: none; text-align: center;">{{ _("Note: Can only update answers on static and regex flag types") }}.<br/><br/></div>
            <form id="stat-answer-form" method="post" action="/admin/view/statistics">
            {% raw xsrf_form_html() %}
            <div style="margin-left: 50px; vertical-align: middle;">
                <input type="hidden" id="answer-flag-uuid" name="flag_uuid" value="" />
                <input type="hidden" id="answer-team-uuid" name="team_uuid" value="" />
                <input type="hidden" id="answer-token" name="answer_token" value="" />
                <p>
                    <input id="acceptanswer" name="accept_answer" type="checkbox" style="margin-top: -5px; margin-right: 5px;" checked> {{ _("Add the answer as an acceptable flag token") }}.
                </p>
                {% if options.penalize_flag_value %}
                <p id="pointrestore">
                    <input name="point_restore" type="checkbox" style="margin-top: -5px; margin-right: 5px;" checked> {{ _("Restore the penalty loss to the ") }}
                    {% if options.teams %}team{% else %}player{% end %} ({% if options.banking %}${% end %}<span id="answer-penalty"></span>{% if not options.banking %} points{% end %}).
                </p>
                {% end %}
            </div>
            </form>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">{{_("Close")}}</a>
            <button id="stat-answer-submit" class="btn btn-primary" type="button">
                <i class="fa fa-fw fa-save"></i>
                {{ _("Save") }}
            </button>
        </div>
    </div>
{% end %}

{% block content %}
{% from models.Corporation import Corporation %}
{% from models.Penalty import Penalty %}
{% from models.Hint import Hint %}
{% from models.Team import Team %}
{% from models.Box import Box %}
    <!-- Start Visible -->
    <div class="row-fluid">
        <div class="span2">
            <ul class="nav nav-tabs nav-stacked affix well">
                {% if len(Corporation.all()) > 1 %}
                    <center>
                        <strong>
                            <i class="fa fa-building-o"></i>
                            {{ _("Corporations") }}
                        </strong>
                    </center>
                    <br />
                    {% for corporation in Corporation.all() %}
                        <li>
                            <a href="#{{ corporation.uuid }}">
                                <i class="icon-chevron-right"></i>
                                &nbsp;&nbsp;{{ corporation.name }}
                            </a>
                        </li>
                    {% end %}
                <br/>
                {% end %}
                <center>
                    <strong>
                        <i class="fa fa-th"></i>
                        {{ _("Boxes") }}
                    </strong>
                </center>
                <br />
                {% for box in Box.all() %}
                    <li>
                        <a href="#{{ box.uuid }}">
                            <i class="icon-chevron-right"></i>
                            &nbsp;&nbsp;{{ box.name }}
                        </a>
                    </li>
                {% end %}
            </ul>
        </div>
        <div class="span10">
            <h1>
                <i class="fa fa-star-o"></i>
                {{ _("Statistics") }}
            </h1>
            <br />
            {% if success is not None and len(success) != 0  %}
                {% for message in success %}
                    <div class="alert alert-success">
                        <a class="close" data-dismiss="alert" href="#">&times;</a>
                        <h4 class="alert-heading">{{ _("SUCCESS") }}</h4>
                        {{ message }}
                    </div>
                {% end %}
            {% end %}
            {% if errors != None and len(errors) != 0 %}
                {% for error in errors %}
                    <div class="alert alert-error">
                        <a class="close" data-dismiss="alert" href="#">&times;</a>
                        <h4 class="alert-heading">{{ _("ERROR") }}</h4>
                        {{ error }}
                    </div>
                {% end %}
            {% end %}
            {% for corp_index, corporation in enumerate(Corporation.all()) %}
            {% if len(Corporation.all()) > 1 %}
                <div id="{{ corporation.uuid }}" class="well">
                    
                    <h2 style="display:inline;">
                        <i class="fa fa-fw fa-building-o"></i>
                        {{ corporation.name }}&nbsp;&nbsp;
                    </h2>
                    
                    <br />
                    <br />
            {% end %}
                    {% if len(corporation.boxes) != 0 %}
                        {% for box in corporation.boxes %}
                            <div id="{{ box.uuid }}" class="well">

                               
                                <div>
                                    <h4 style="display:inline;">
                                        <i class="fa fa-flag-o"></i>
                                        {{ _("Flags on") }} {{ box.name }}&nbsp;&nbsp;&nbsp;
                                    </h4>
                                    {% if len(Hint.taken_by_box(box.id)) > 0 %}<div style="float: right; vertical-align: bottom;">{{ _("Box Hints Taken") }}: <span style="color: rgb(200,200,0);">{{ len(Hint.taken_by_box(box.id)) }}</span></div>{% end %}
                                    <hr>
                                    {% if len(box.flags) != 0 %}
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <th>#</th>
                                                    <th>{{ _("Box") }}</th>
                                                    <th><!-- Flag Lock --></th>
                                                    <th>{{ _("Name") }}</th>
                                                    <th class="shortcolumn" style="width: 15%; text-align: center;">{{ _("Capture Rate") }}</th>
                                                    <th class="shortcolumn" style="width: 15%; text-align: center;">{{ _("Successful Captures") }}</th>
                                                    <th class="shortcolumn" style="width: 15%; text-align: center;">{{ _("Failed Attempts") }}</th>
                                                    <th class="shortcolumn" style="width: 15%; text-align: center;">{{ _("Hints Taken") }}</th>
                                                    <th><!-- Edit Button --></th>
                                                    
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% set teamcount = len(Team.all()) %}
                                                {% for index, flag in enumerate(box.flags) %}
                                                    <tr>
                                                        {% set captures = len(Flag.team_captures(flag.id)) %}
                                                        <td class="shortcolumn">{{ index + 1 }}</td>
                                                        <td class="shortcolumn">{{ flag.box.name }}</td>
                                                        <td class="shortcolumn" style="padding-right: 0px;">{% if flag.lock_id %}<i class="fa fa-lock" title="Locked by {{ Flag.by_id(flag.lock_id).name }}"></i>{% end %}</td>
                                                        <td style="width: 40%; padding-right: 25px;" title="{{flag.description}}">{{ flag.name }}</td>
                                                        <td class="shortcolumn" style="text-align: center;">{% if captures > 0 %}{{ int(float(captures) / float(teamcount) * 100) }}%{% else %}0%{% end %}</td>
                                                        <td class="shortcolumn" style="text-align: center;{% if captures > 0 %} color: green;{% end %}">{{ captures }}</td>
                                                        <td class="shortcolumn" style="text-align: center;{% if len(Penalty.by_flag_id(flag.id)) > 0 %} color: rgb(200, 0, 0);{% end %}">{{ len(Penalty.by_flag_id(flag.id)) }}</td>
                                                        <td class="shortcolumn" style="text-align: center;{% if len(Hint.taken_by_flag(flag.id)) > 0 %} color: rgb(200, 200, 0);{% end %}">{{ len(Hint.taken_by_flag(flag.id)) }}</td>
                                                        
                                                        <td class="shortcolumn">
                                                            <a id="details-flag-button-{{ flag.uuid }}" class="btn btn-mini" data-toggle="modal" href="#details-flag-modal" data-uuid="{{ flag.uuid }}" data-box-uuid="{{ flag.box.uuid }}" title="{{flag.token}}">
                                                                <i class="fa fa-fw fa-eye"></i>
                                                                {{ _("View Details") }}
                                                            </a>
                                                        </td>
                                                       
                                                    </tr>
                                                {% end %}
                                            </tbody>
                                        </table>
                                    {% else %}
                                        <strong>{{ _("No flags on this box yet") }}, <a href="/admin/create/flag">{{ _("add some") }}</a></strong>
                                    {% end %}
                                </div>
                                
                            </div>
                            <br />
                        {% end %}
                    {% else %}
                        <strong>{{ _("No boxes in this corporation yet") }}, <a href="/admin/create/box">{{ _("add some") }}</a></strong>
                    {% end %}
                {% if len(Corporation.all()) > 1 %}
                </div>
                {% end %}
            {% end %}
        </div>
    </div>
{% end %}